package com.eusoft.book_manage.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.eusoft.book_manage.entity.User;
import com.eusoft.book_manage.service.UserService;
import com.eusoft.book_manage.util.JWTUtils;
import com.eusoft.book_manage.util.Result;
import com.eusoft.book_manage.util.ResultUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author eusoft
 * @date 2021/10/20 15:24
 */
@RestController
public class PublicController {

    @Resource
    private UserService userService;

    @GetMapping("/getCurrentUserInfo")
    public Result<Object> getUserInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        DecodedJWT jwt = JWTUtils.getToken(token);
        Integer aid = jwt.getClaim("aid").asInt();
        try{
            User user = userService.getUserByAid(aid);
            Map<String,String> data = new HashMap<>();
            data.put("name",user.getName());
            return ResultUtil.success(data);
        }
        catch (Exception e){
            System.out.println(e.getMessage());
            return ResultUtil.error(-1,e.getMessage(),null);
        }
    }

}
